<?php
// SourceBB Ban Inport

// Get current UNIX timestamp in miliseconds so we can calculate script execution time
$time_start = microtime(true);

// Connect to the MySQL database
$connection = mysql_connect('localhost', 'root', '');

// Check if the connection was successful
if (!$connection) {
    die('Couldn\'t connect to the remote MySQL database');
}

// Select the SourceBB database
$database = mysql_select_db('sourcebbui', $connection);

// Check if the database exists
if (!$database) {
    die ('Couldn\'t select the SourceBB database');
}

// Load user's SQL file into an array
$dump = file('sb_bans.sql', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

// Make sure that the file is loaded correctly
if (!$dump) {
	die('Error loading SQL file');
}

// Check if the file actually contains the SourceBans information
$start = array_search('INSERT INTO `sb_bans` (`bid`, `ip`, `authid`, `name`, `created`, `ends`, `length`, `reason`, `aid`, `adminIp`, `sid`, `country`, `RemovedBy`, `RemoveType`, `RemovedOn`, `type`, `ureason`) VALUES', $dump);

// Stop the script if we can't find it
if (!$start) {
	die('Couldn\'t find any SourceBans information in SQL file');
}

// Our main loop for getting the ban data
for ($i = $start; $i < count($dump); $i++) {
	// Find a way around phpMyAdmin's query limit
	if ($dump[$i] == 'INSERT INTO `sb_bans` (`bid`, `ip`, `authid`, `name`, `created`, `ends`, `length`, `reason`, `aid`, `adminIp`, `sid`, `country`, `RemovedBy`, `RemoveType`, `RemovedOn`, `type`, `ureason`) VALUES' && $i != $start) {
		continue;
	}
	
	// Replace semicolon with a seperator to avoid parse errors
	if ($i != $start) {
		$dump[$i] = substr($dump[$i], 0, -1) . ','; 
	}
	
	// Put eash ban information into an array
	$bans[$i] = explode(',', $dump[$i]);
	
	// Clean the array
	foreach ($bans[$i] as &$string) {
		$string = str_replace(array('\'', '`'), '', $string);
		$string = trim($string);
	}
	
	// Remove SourceBans' ID field
	unset($bans[$i][0]);
	
	// Don't parse the field information key
	if ($i != $start) {
		array_pop($bans[$i]);
		$bans[$i][count($bans[$i])] = substr($bans[$i][count($bans[$i])], 0, -1);
	} else {
		$bans[$i][count($bans[$i])] = substr($bans[$i][count($bans[$i])], 0, -8);
	}
	
	// Re-index array keys
	$bans[$i] = array_values($bans[$i]);
}

// Re-index array keys
$bans = array_values($bans);

// Assign field information to array keys
for ($i = 1; $i < count($bans); $i++) {
	$counter = 0;
	foreach ($bans[$i] as $string) {
		$sbb[$i-1][$bans[0][$counter]] = $string;
		$counter++;
	}
}

// Insert the date into the MySQL database
for ($i = 0; $i < count($sbb); $i++) {
	// Make data safe for the query
	$steam_id = sanitizeString($sbb[$i]['authid'], $connection);
	$player_name = sanitizeString($sbb[$i]['name'], $connection);
	$ipaddr = sanitizeString($sbb[$i]['ip'], $connection);
	$ban_length = sanitizeString($sbb[$i]['length'], $connection);
	$ban_reason = sanitizeString($sbb[$i]['reason'], $connection);
	
	// Check if the IP address is set
	if ($ipaddr == null) {
		mysql_query("INSERT INTO mysql_bans (steam_id, player_name, ipaddr, ban_length, ban_reason, banned_by, banned_by_id) VALUES ('$steam_id', '$player_name', NULL, '$ban_length', '$ban_reason', 'Console', NULL)");
	} else {
		mysql_query("INSERT INTO mysql_bans (steam_id, player_name, ipaddr, ban_length, ban_reason, banned_by, banned_by_id) VALUES ('$steam_id', '$player_name', '$ipaddr', '$ban_length', '$ban_reason', 'Console', NULL)");
	}
}

// Calculate script execution time
$time_end = microtime(true);
$time = $time_end - $time_start;

// Display the results
echo 'Sucssesfully imported <b>' . count($sbb) . '</b> bans in <b>' . round($time, 5) . '</b> seconds';
?>